# =========== 导入组件 ============
from rest_framework import serializers
# =========== 导入数据类 ============
from basicweb.models import BranchArea, AreaPlace, Branch, TaskStatus, TaskType, PlaceType
from useweb.models import Staff



# 场地类型
class PlaceTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model =PlaceType
        fields = "__all__"



# 任务类行
class TaskTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model =TaskType
        fields = "__all__"


# 任务状态
class TaskStatusSerializer(serializers.ModelSerializer):
    class Meta:
        model = TaskStatus
        fields = "__all__"

# 分部
class BranchSerializer(serializers.ModelSerializer):

    class Meta:
        model = Branch
        fields = "__all__"


# 区域
class BranchAreaSerializer(serializers.ModelSerializer):
    branch = BranchSerializer()
    class Meta:
        model = BranchArea
        fields = "__all__"


# 场地
class AreaPlaceSerializer(serializers.ModelSerializer):
    brancharea = BranchAreaSerializer()
    staffs = serializers.SerializerMethodField()  # 这个字段的值由后续的方法填充
    staffobj =  serializers.SerializerMethodField()   # 所有学员的对象  [{950001:dddd},{950002：3dddd}]
    class Meta:
        model = AreaPlace
        fields = "__all__"


    def get_staffs(self, obj):
        # 获取所有的场地负责人
        staff_objs = Staff.objects.filter(areaplace=obj.code)
        # 定义code集合
        staff_name_list = []
        # 遍历获取的集合
        for item in staff_objs:
            staff_name_list.append(item.name)
        # 返回
        return " ".join(staff_name_list)


    def get_staffobj(self, obj):
        # 获取所有的场地负责人
        staff_objs = list(Staff.objects.filter(areaplace=obj.code).values("id", 'name'))
        # 定义code集合
        return staff_objs


